package com.daka.util;

import ch.qos.logback.core.util.TimeUtil;
import org.h2.util.DateTimeUtils;
import sun.util.calendar.CalendarUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CalendarDemo {
    public static void main(String[] args) throws ParseException {

        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());

        System.out.println(calendar.getFirstDayOfWeek());
//        System.out.println(calendar.getCalendarType());   gregory
        System.out.println(calendar.get(Calendar.DATE));   // 15
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));   // 15


        int y = DateTimeUtils.getDayOfWeek(calendar.get(Calendar.DATE), 2);
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));   // 4
        System.out.println(y);   // 15

        y = DateTimeUtils.getWeekOfYear(calendar.get(Calendar.DATE), 1, 7);
        System.out.println(y);   // 15
        y = DateTimeUtils.getSundayDayOfWeek(calendar.get(Calendar.DATE));
        System.out.println(y);   // 15

        LocalDateTime ldt = LocalDateTime.now();
        Date date = DateFormat.getDateInstance().parse(getWeekStartCorrect());
        System.out.println(ldt.getDayOfWeek().getValue());

        System.out.println(date);

//        TimeUtil.computeStartOfNextDay()

    }

    /**
     * 获取本周的第一天
     *
     * @return String
     **/
    public static String getWeekStartCorrect() {
        int num;
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayOfWeek == 1) {
            num = 0;
        } else if (dayOfWeek == 0) {
            num = -6;
        } else {
            num = 1 - dayOfWeek;
        }
        calendar.add(Calendar.DATE, num);
        Date monday = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(monday);
    }
}
